home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 58606 / 58606.xpi / chrome / translator.jar / content / prefsManager.js < prev    next >
Text File  |  2010-02-07  |  2KB  |  100 lines

  1.  
  2. (function(namespace, $)
  3. {
  4.     namespace.PrefsManager = function()
  5.     {
  6.         this.service = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.translator.");
  7.         this.service.QueryInterface(Components.interfaces.nsIPrefBranch2);
  8.         this.service.addObserver('', this, false);
  9.     };
  10.     
  11.     namespace.PrefsManager.prototype = {
  12.         service: null,
  13.         
  14.         getPref: function(pref)
  15.         {
  16.             if(this.service == null) {
  17.                 throw new Exception('Service not initialized');
  18.             }
  19.             
  20.             var prefType = this.getPrefType(pref);
  21.             
  22.             switch(prefType) {
  23.                 case this.service.PREF_BOOL:
  24.                     return this.service.getBoolPref(pref);
  25.                     
  26.                 case this.service.PREF_INT:
  27.                     return this.service.getIntPref(pref);
  28.                     
  29.                 case this.service.PREF_STRING:
  30.                     return this.service.getCharPref(pref);
  31.             }
  32.             
  33.             return null;
  34.         },
  35.         
  36.         getAllPrefs: function()
  37.         {
  38.             var prefs = {};
  39.             var prefKeys = this.service.getChildList('', {});
  40.             
  41.             $.each(prefKeys, function(i, prefKey) {
  42.                 prefs[prefKey] = this.getPref(prefKey);
  43.             }.bind(this));
  44.             
  45.             return prefs;
  46.         },
  47.         
  48.         setPref: function(pref, value, type)
  49.         {
  50.             if(this.service == null) {
  51.                 throw new Exception('Service not initialized');
  52.             }
  53.             
  54.             var prefType = type || this.getPrefType(pref);
  55.             
  56.             switch(prefType) {
  57.                 case this.service.PREF_BOOL:
  58.                     this.service.setBoolPref(pref, value);
  59.                     break;
  60.                     
  61.                 case this.service.PREF_INT:
  62.                     this.service.setIntPref(pref, value);
  63.                     break;
  64.                     
  65.                 case this.service.PREF_STRING:
  66.                     this.service.setCharPref(pref, value);
  67.                     break;
  68.             }
  69.         },
  70.         
  71.         getPrefType: function(pref)
  72.         {
  73.             return this.service.getPrefType(pref);
  74.         },
  75.         
  76.         resetAllPrefs: function()
  77.         {
  78.             var preferences = this.getAllPrefs();
  79.             
  80.             $.each(preferences, function(k, v) {
  81.                 // continue if preference key starts from '_' which
  82.                 // means that it's internal preference and should not be defaulted
  83.                 if(k.substr(0, 1) == '_') return true;
  84.                 
  85.                 if(this.service.prefHasUserValue(k)) {
  86.                     this.service.clearUserPref(k);
  87.                 }
  88.             }.bind(this));
  89.         },
  90.         
  91.         observe: function(subject, topic, data)
  92.         {
  93.             // check if document exists because if prefs window
  94.             // already closed it's throwing js error (see #44)
  95.             if(topic != 'nsPref:changed' || !document) return;
  96.             
  97.             $(document).trigger('translatorPreferencesChanged.translator');
  98.         },
  99.     };
  100. })(com.igorgladkov.translator, translatorJQuery);